﻿using System;
using System.Linq;
using SpCop.Core.Model;
using SpCop.Model;

namespace SpCop.General
{
    class EnsureDependenciesWhenDeployingToStyleLibrary : ModuleRuleBase
    {
        private static readonly Guid SharePointPublishingSite =  new Guid("{f6924d36-2fa8-4f0b-b16d-06b7250180fa}");

        public override void Visit(Feature item)
        {
            var xAttribute = item.Xml.Attribute("Hidden");
            
            // if the fature is hidden its not a problem power users wont be able to see it.
            if(xAttribute != null && xAttribute.Value == "TRUE")
                return;
            
            
            base.Visit(item);
        }

        protected override void VisitModule(Package package, Feature feature, Element item)
        {
            if (item == null ) return;

            var url = item.Xml.Attribute("Url").Value;


            if ((url == "Style Library" || url == "SiteCollectionImages" || url == "$Resources:cmscore,List_Pages_UrlName;" || url=="Pages") && !feature.Dependencies.Any(a => a.Id == SharePointPublishingSite))
            {
               
                AddProblem("DependencyNotFound",feature.Title,item.Xml.Attribute("Name").Value);
            }
            else
            {
                
            }
        }
    }
}